package com.cn.thread.blocking;

import com.cn.thread.common.BufferResources;

import java.util.concurrent.ArrayBlockingQueue;

public class BlockingBufferResources implements BufferResources {

    public static final int MAX_CAPACITY = 10;
    ArrayBlockingQueue<Integer> buffer = new ArrayBlockingQueue<>(MAX_CAPACITY);


    public void consume() {
        try {
            Integer value = (Integer) buffer.take();
            System.out.println(Thread.currentThread().getName() + " 消费成功：" + value.toString() + " 当前缓冲区size = " + buffer.size());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void product(Integer value) {
        try {
            buffer.put(value);
            System.out.println(Thread.currentThread().getName() + " 生产成功：" + value.toString() + " 当前缓冲区size = " + buffer.size());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
